package com.gmail.avereshchaga.db.service;

import com.gmail.avereshchaga.db.dao.IUserSessionIdDAO;
import com.gmail.avereshchaga.db.entity.IUserSessionId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@Service
public class UserSessionIdServiceImpl implements IUserSessionIdService {

    @Autowired
    private IUserSessionIdDAO userSessionIdDAO;

    @Transactional
    @Override
    public void createUserSessionId(IUserSessionId userSessionId) {
        userSessionIdDAO.createUserSessionId(userSessionId);
    }

    @Transactional
    @Override
    public boolean findBySessionId(String sessionIdStr) {
        Long sessionId = Long.parseLong(sessionIdStr);
        if (!(userSessionIdDAO.findBySessionId(sessionId) == null)) {
            return true;
        } else {
            return false;
        }
    }

    @Transactional
    @Override
    public void closeSession(String username, String password) {
        userSessionIdDAO.closeSession(username, password);
    }

    @Transactional
    @Override
    public IUserSessionId findUserBySessionId(String sessionIdStr) {
        Long sessionId = Long.parseLong(sessionIdStr);
        IUserSessionId userSessionId = userSessionIdDAO.findBySessionId(sessionId);
        return userSessionId;
    }
}
